package demo1.Adaptee;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @ClassName ObjectAdapterTest  对象适配器
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/6/2 15:43
 * @Version 1.0
 */
// 适配器模式------> 将一个接口转换为另一个接口（目标接口）。 用户无需关心 被适配的是什么。
public class ObjectAdapterTest {
    public static void main(String[] args) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        Target1 create = FactoryClass.Create(ObjectAdapter.class);
        create.response();
        // 包装了被适配对象，使其不可见  只依赖目标接口
    }
}
// 目标接口
interface Target1{
    void response();
}
// 被适配的类
class Adapter1{
    public void specificRequest(){
        System.out.println("适配者的代码调用");
    }
}
// 对象适配器1
class ObjectAdapter implements Target1{

    private Adapter1 adapter1;

    public ObjectAdapter(Adapter1 adapter1){
        this.adapter1 = adapter1;
    }

    public ObjectAdapter(){
    }

    @Override
    public void response() {
        System.out.println("方法执行前");
        adapter1.specificRequest();
        System.out.println("方法执行后");
    }
}

// 对象适配器2

class FactoryClass{
    public static Target1 Create(Class tClas) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        Constructor constructor = tClas.getConstructor(Adapter1.class);
        return (Target1) constructor.newInstance(new Adapter1());


    }
}